//
//  HMHomeOriginalView.swift
//  Weibo
//
//  Created by itteacher on 2017/7/26.
//  Copyright © 2017年 itteacher. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage
import YYText

//  自定义原创微博视图
class HMHomeOriginalView: UIView {
    
    //  记录原创为微博底部约束
    var originalViewBottomConstraint: Constraint?
    
    //  数据模型
    var statusViewModel: HMStatusViewModel? {
        didSet {
            
            if let imageUrl = statusViewModel?.status?.user?.profile_image_url {
                //  显示头像
                userImageView.sd_setImage(with: URL(string: imageUrl), placeholderImage: UIImage(named: "avatar_default_big"))
            }
            screenNameLabel.text = statusViewModel?.status?.user?.screen_name
//            contentLabel.text = statusViewModel?.status?.text
            //  设置富文本
            contentLabel.attributedText = statusViewModel?.originalAttributedStr
            
            mbrankImageView.image = statusViewModel?.mbrankImage
            verifiedTypeImageView.image = statusViewModel?.verifiedTypeImage
            sourceLabel.text = statusViewModel?.sourceContent
            timeLabel.text = statusViewModel?.timeContent
            
            //  卸载之前的原创微博视图的底部约束
            originalViewBottomConstraint?.deactivate()
            
            if let picUrls = statusViewModel?.status?.pic_urls, picUrls.count > 0 {
                //  有原创微博配图
                //  1. 显示配图
                pictureView.isHidden = false
                //  2. 重新创建原创微博底部约束
                self.snp.makeConstraints({ (make) in
                    originalViewBottomConstraint = make.bottom.equalTo(pictureView.snp.bottom).offset(HomeCellMargin).constraint
                })
                //  3. 给原创微博配图绑定数据
                pictureView.picUrls = picUrls
                
            } else {
                //  没有转发微博配图
                //  1. 隐藏配图
                pictureView.isHidden = true
                //  2. 重新创建原创微博底部约束
                self.snp.makeConstraints({ (make) in
                    originalViewBottomConstraint = make.bottom.equalTo(contentLabel.snp.bottom).offset(HomeCellMargin).constraint
                })
            }
            
        }
    }
    
    //  MARK: --懒加载控件
    //  用户头像
    fileprivate lazy var userImageView: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    //  认证类型图片
    fileprivate lazy var verifiedTypeImageView: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    //  昵称
    fileprivate lazy var screenNameLabel: UILabel = {
        let label = UILabel(fontSize: 14, textColor: UIColor.darkGray)
        label.text = "趣闻搞笑"
        return label
    }()
    //  时间
    fileprivate lazy var timeLabel: UILabel = {
        let label = UILabel(fontSize: 12, textColor: UIColor.orange)
        label.text = "刚刚"
        
        return label
    }()
    
    //  来源
    fileprivate lazy var sourceLabel: UILabel = {
        let label = UILabel(fontSize: 12, textColor: UIColor.lightGray)
        label.text = "来自: 星星的你"
        
        return label
    }()
    //  会员等级图片
    fileprivate lazy var mbrankImageView: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))

    //  微博内容
    fileprivate lazy var contentLabel: YYLabel = {
        let label = YYLabel()
        //  设置最大换行的宽度
        label.preferredMaxLayoutWidth = ScreenWidth - 2 * HomeCellMargin
        //  多行显示
        label.numberOfLines = 0
        
        return label
    }()
    
    //  配图
    fileprivate lazy var pictureView: HMHomePictureView = {
        let collectionView = HMHomePictureView()
        //  配图颜色和转发微博颜色一样
        collectionView.backgroundColor = self.backgroundColor
        return collectionView
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //  添加控件设置约束
    private func setupUI() {
        backgroundColor = UIColor.white
        
        addSubview(userImageView)
        addSubview(verifiedTypeImageView)
        addSubview(screenNameLabel)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(mbrankImageView)
        addSubview(contentLabel)
        addSubview(pictureView)
        
        userImageView.snp.makeConstraints { (make) in
            make.top.leading.equalTo(self).offset(HomeCellMargin)
            //  首先userImageView有一个顶部约束，然后contentLabel的顶部等于userImageView的底部，那么这样会形成一个高度，而我们自己设置了userImageView的大小，就会出现高度重复创建，解决办法：设置约束优先级，覆盖重复的约束
            make.size.equalTo(CGSize(width: 35, height: 35)).priority(.high)
        }
        verifiedTypeImageView.snp.makeConstraints { (make) in
            make.centerX.equalTo(userImageView.snp.trailing)
            make.centerY.equalTo(userImageView.snp.bottom)
        }
        screenNameLabel.snp.makeConstraints { (make) in
            make.top.equalTo(userImageView)
            make.leading.equalTo(userImageView.snp.trailing).offset(HomeCellMargin)
        }
        timeLabel.snp.makeConstraints { (make) in
            make.leading.equalTo(screenNameLabel)
            make.bottom.equalTo(userImageView)
        }
        sourceLabel.snp.makeConstraints { (make) in
            make.leading.equalTo(timeLabel.snp.trailing).offset(HomeCellMargin)
            make.bottom.equalTo(timeLabel)
        }
        mbrankImageView.snp.makeConstraints { (make) in
            make.top.equalTo(screenNameLabel)
            make.leading.equalTo(screenNameLabel.snp.trailing).offset(HomeCellMargin)
        }
        contentLabel.snp.makeConstraints { (make) in
            make.top.equalTo(userImageView.snp.bottom).offset(HomeCellMargin)
            make.leading.equalTo(userImageView)
            make.trailing.equalTo(self).offset(-HomeCellMargin)
        }
        
        pictureView.snp.makeConstraints { (make) in
            make.top.equalTo(contentLabel.snp.bottom).offset(HomeCellMargin)
            make.leading.equalTo(contentLabel)
            //  设置默认大小，后续根据图片张数来确定真是的配图大小
            make.size.equalTo(CGSize(width: 100, height: 100)).priority(.high)
        }
        
        //  关键约束
        self.snp.makeConstraints { (make) in
            originalViewBottomConstraint = make.bottom.equalTo(pictureView).offset(HomeCellMargin).constraint
        }
        
        //  设置高亮文字点击的闭包
        contentLabel.highlightTapAction = { (view, attributeStr, range, rect) in
            //  view: 点击富文本对应的视图-> YYLabel
            //  attributeStr: 点击视图显示的富文本
            //  range: 点击高亮文字的范围
            //  rect: 点击高亮视图区域
            //  获取指定范围的富文本
            let subAttributedStr = attributeStr.attributedSubstring(from: range)
            //  获取富文本对应的字符串
            let result = subAttributedStr.string
            print(result)
           
        }
    }

}
